package com.orhon.smartcampus.modules.teacher.mutation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.teacher.dao.PassportAuditDao;
import com.orhon.smartcampus.modules.teacher.dao.PassportDao;
import com.orhon.smartcampus.modules.teacher.entity.Passport;
import com.orhon.smartcampus.modules.teacher.entity.PassportAudit;
import com.orhon.smartcampus.modules.teacher.gqlInputModel.GQLPassportAuditInput;
import com.orhon.smartcampus.modules.teacher.inputMapper.PassportAuditInputMapper;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddPassportAuditMutation implements DataFetcher {

    @Autowired
    PassportAuditDao passportAuditDao;

    @Autowired
    PassportDao passportDao;

    /**
     * 添加护照审核
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {
        GQLPassportAuditInput input = PassportAuditInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        PassportAudit passportAudit = null;

        if (input.getId() != null) {
            passportAudit = passportAuditDao.get(PassportAudit.class, input.getId());
            passportAudit.setUpdatedAt(new Date());
        } else {
            passportAudit = new PassportAudit();
            passportAudit.setCreatedAt(new Date());
        }
        if(input.getPassport_id() != null) {
            passportAudit.setTeacherPassportByPassportId( passportDao.get(Passport.class, input.getPassport_id()));
        }
        if(input.getCompid() != null){

            passportAudit.setCompid(input.getCompid());
        }
        if(input.getCompanyopinion() != null){

            passportAudit.setCompanyopinion(JSON.toJSONString(input.getCompanyopinion()));
        }
        if(input.getDirectorcompanyopinion() != null){

            passportAudit.setDirectorcompanyopinion(JSON.toJSONString(input.getDirectorcompanyopinion()));
        }
        try{
            passportAuditDao.saveOrUpdate(passportAudit);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }
        return Result.ok();
    }
}

